//
//  SHPEmoticonBottom.swift
//  weibo
//
//  Created by songhuipan on 16/4/25.
//  Copyright © 2016年 songhuipan. All rights reserved.
//

import UIKit
enum ButtonType:Int{
    case Default = 0
    case Emoji = 1
    case Lxh = 2
}
let tagNum = 1000
class SHPEmoticonBottom: UIView {
    
    var bottomCloseure:((type:ButtonType)->())?
    
    var currentButton:UIButton?

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
extension SHPEmoticonBottom {
    // MARK:滑动cell时设置当前按钮
    func setCurrentSelectedButton(section:Int){
        // 系统控件默认有一个tag值为0
        let button = viewWithTag(section + tagNum) as! UIButton
        
        // 不能使用这种方法，会因为闭包的原因使得执行切换区域的效果，不能滑动到下一个区域了
//        buttonClick(button)
        if currentButton == button{
            return
        }
        currentButton?.selected = false
        button.selected = true
        currentButton = button
    }
    // MARK:点击底部按钮，执行闭包
    func buttonClick(sender:UIButton){
        if currentButton == sender{
            return
        }
        currentButton?.selected = false
        sender.selected = true
        currentButton = sender
        bottomCloseure!(type:ButtonType(rawValue: sender.tag - tagNum)!)
    }
    // MARK:设置视图UI
    func setupUI(){
        let defaultButton = addChildrenButton("默认", imageName: "compose_emotion_table_left", type: .Default)
        let emojiButton = addChildrenButton("emoji", imageName: "compose_emotion_table_mid", type: .Emoji)
        let lxhButton = addChildrenButton("浪小花", imageName: "compose_emotion_table_right", type: ButtonType.Lxh)
        defaultButton.snp_makeConstraints { (make) -> Void in
            make.leading.bottom.top.equalTo(self)
            make.width.equalTo(emojiButton)
        }
        emojiButton.snp_makeConstraints { (make) -> Void in
            make.top.bottom.equalTo(self)
            make.leading.equalTo(defaultButton.snp_trailing)
            make.width.equalTo(lxhButton)
        }
        lxhButton.snp_makeConstraints { (make) -> Void in
            make.top.bottom.trailing.equalTo(self)
            make.leading.equalTo(emojiButton.snp_trailing)
        }
        // 设置默认选中按钮
        defaultButton.selected = true
        currentButton = defaultButton
    }
    // MARK:添加按钮的方法
    func addChildrenButton(title:String,imageName:String,type:ButtonType)->UIButton {
        let button = UIButton(type: UIButtonType.Custom)
        button.tag = type.rawValue + tagNum
        button.addTarget(self, action: "buttonClick:", forControlEvents: UIControlEvents.TouchUpInside)
        button.setBackgroundImage(UIImage(named: "\(imageName)_normal"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "\(imageName)_selected"), forState: UIControlState.Selected)
        button.setTitle(title, forState: UIControlState.Normal)
        button.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
        button.setTitleColor(UIColor.grayColor(), forState: UIControlState.Selected)
        button.titleLabel?.font = UIFont.systemFontOfSize(15)
        
        addSubview(button)
        return button
        
    }
}

















